package com.mico.example.springboot.mybatis.redis.config;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Created by micocube
 * ProjectName: springboot-example
 * PackageName: com.mico.example.springboot.redis.config
 * User: micocube
 * Email: ldscube@gmail.com
 * CreateTime: 2019/2/28上午9:52
 * ModifyTime: 2019/2/28上午9:52
 * Version: 0.1
 * Description:
 **/
@Configuration
@EnableAutoConfiguration
public class RedisConfig {


    @Bean
    @ConfigurationProperties(prefix = "spring.redis")
    public JedisPoolConfig poolConfig() {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        return poolConfig;
    }


    @Bean
    public JedisConnectionFactory redisConnectionFactory() {
        return new JedisConnectionFactory(poolConfig());
    }


    @Bean
    public RedisTemplate redisTemplate() {
        // 注意这里使用的是StringRedisTempalte
        StringRedisTemplate template = new StringRedisTemplate(redisConnectionFactory());
        GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
        // 设置值的序列化器
        template.setValueSerializer(jackson2JsonRedisSerializer);
        return template;
    }
}
